﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;

public partial class map_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    private int m_ParentNodeId = 40;
    //合作單位討論區
    private int m_NodeId = 41;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_NodeId = QueryStringHelper.GetInteger("node", 0);

        if (!IsPostBack)
        {            
            if (m_NodeId == 0)
            {
                IList<NodeVO> nodeList = m_PostService.GetNodeListByParentId(m_ParentNodeId);                
                if (nodeList != null && nodeList.Count() > 0)
                {                    
                    Response.Redirect(string.Format("discuss.aspx?node={0}", nodeList[0].NodeId));
                }
            }

            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            if (nodeVO != null)
            {
                ltlName.Text = nodeVO.Name;
                ltlContent.Text = nodeVO.HtmlContent;
                btnSearch_Click(null, null);
            }
        }
    }

    private void FillList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", m_NodeId.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("KeyWord", txtSearchKeyWord.Text.Trim());
        conditions.Add("Order", "order by p.CreatedDate desc ");

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);        
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        RepeaterList.DataSource = m_PostService.GetPostList(conditions);
        RepeaterList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillList();
    }

    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            Dictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("ParentPostId", postVO.PostId.ToString());
            conditions.Add("Flag", "1");            

            int replyCount = m_PostService.GetPostCount(conditions);
            UIHelper.SetLiteralText(ctrl, "ltlReplyCount", replyCount.ToString());
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        FillList();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("add_discuss.aspx?node={0}", m_NodeId));
    }
}